package com.example.wm.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wm.common.R;
import com.example.wm.common.Result;
import com.example.wm.common.ResultData;

import com.example.wm.entity.Diban;
import com.example.wm.entity.Employee;
import com.example.wm.mapper.DibanMapper;
import com.example.wm.service.DibanService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController

@RequestMapping("/api")
@CrossOrigin
public class DibanContoller {
    @Resource
    private DibanService dibanService;
    private DibanMapper dibanMapper;
    @Transactional
    @PostMapping("/dibancompared")
    public Result getById(@RequestBody Diban diban){
//        Employee ee=employeeService.getById(employee.getId());
        LambdaQueryWrapper<Diban> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.ne(Diban::get货主,"垃圾")
                .eq(Diban::get类型,"进库")
                .ne(Diban::get过磅状态,"已作废")
                .eq(Diban::get磅房,diban.get磅房())
                .apply("date_format(判定时间,'%y-%m-%d')=date_format(now(),'%y-%m-%d')");
        List<Diban> list = dibanService.list(queryWrapper);
//        Diban emp=dibanService.getObj( queryWrapper);

        return ResultData.success("操作成功",list);
    }

    @Transactional
    @PostMapping("/addnewMassge")
    public Result addnewMassge(@RequestBody Diban diban){
            Boolean a=  dibanService.save(diban);
        return ResultData.success(a);
    }
    @Transactional
    @PostMapping("/findbycartnum")
    public Result findbycartnum(@RequestBody JSONObject jsonObject){
        LambdaQueryWrapper<Diban> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.ne(Diban::get货主,"垃圾")
                .eq(Diban::get类型,"进库")
                .eq(Diban::get定价状态,"待定价")
                .eq(Diban::get过磅状态,"已完成")
//                .eq(Diban::get磅房,jsonObject.getString("磅房"))
                .apply("date_format(一次过磅时间,'%y-%m')=date_format(now(),'%y-%m')")
                .like(Diban::get车号,jsonObject.getString("车号"))
                .orderByDesc(Diban::get一次过磅时间)
        ;
        List<Diban> list = dibanService.list(queryWrapper);
        return ResultData.success(list);
    }
    //分页查询
    @PostMapping("/page")
    @Transactional
    public R<Page> page(@RequestBody JSONObject jsonObject) {
        Page<Diban> pageInfo = new Page<>(jsonObject.getInteger("page"),
                jsonObject.getInteger("pageSize"));
        LambdaQueryWrapper<Diban> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.ne(Diban::get货主,"垃圾")
                .eq(Diban::get类型,"进库")
                .eq(Diban::get定价状态,"待定价")
                .eq(Diban::get过磅状态,"已完成")
                .eq(Diban::get磅房,jsonObject.getString("name"));
      Page<Diban> dl= dibanService.page(pageInfo,queryWrapper);
        return R.success(dl);
    }

    @Transactional
    @PostMapping("/findall")
    public Result findall(@RequestBody JSONObject jsonObject){
        LambdaQueryWrapper<Diban> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.ne(Diban::get货主,"垃圾")
                .eq(Diban::get类型,"进库")
                .eq(Diban::get定价状态,"待定价")
                .eq(Diban::get过磅状态,"已完成")
                .eq(Diban::get磅房,jsonObject.getString("磅房"))
//                .orderByDesc(Diban::get一次过磅时间)

                .apply("date_format(一次过磅时间,'%y-%m')=date_format(now(),'%y-%m')");
        List<Diban> list = dibanService.list(queryWrapper);
        return ResultData.success(list);
    }
    //根据id
    @Transactional
    @PostMapping("/findbyid")
    public Result findbyid(@RequestBody JSONObject jsonObject){
        LambdaQueryWrapper<Diban> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.ne(Diban::get货主,"垃圾")
                .eq(Diban::get类型,"进库")
                .eq(Diban::get定价状态,"待定价")
                .eq(Diban::get过磅状态,"已完成")
                .eq(Diban::get磅房,jsonObject.getString("磅房"))
                .eq(Diban::getId,jsonObject.getInteger("id"))
//                .orderByDesc(Diban::get一次过磅时间)

                .apply("date_format(一次过磅时间,'%y-%m')=date_format(now(),'%y-%m')");
        List<Diban> list = dibanService.list(queryWrapper);
        return ResultData.success(list);
    }
    @Transactional
    @PostMapping("/update")
    public Result update(@RequestBody Diban diban){
         Boolean a=    dibanService.updateById(diban);
        return ResultData.success(a);
    }
//    http://47.115.29.137:82/cddbzy/tyypd/

}
